<?php
/**
 * Joomla! 1.5 component JoomGPSTracks
 *
 * @version $Id: terrain.php 2009-08-09 15:40:19 svn $
 * @author Michael Pfister
 * @package joomGPStracks
 * @subpackage backend
 * @license GNU/GPL
 * @filesource
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

// Import Joomla! libraries
jimport('joomla.application.component.model');
/**
 * Model Class Terrain
 */
class JoomgpstracksModelTerrain extends JModel  {

    var $_data = null;

    var $_total = null;

    var $_pagination = null;

    function __construct() {
        parent::__construct();
        global $mainframe, $option;

        // Get the pagination request variables
        $limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
        $limitstart	= $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );

        // In case limit has been changed, adjust limitstart accordingly
        $limitstart = JRequest::getVar('limitstart',0);

        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);

        $array = JRequest::getVar('cid', array(0), '', 'array');
        $edit	= JRequest::getVar('edit',true);
        if($edit)
                $this->setId((int)$array[0]);
    }

        /**
         *
         * @return object
         */
	function getData()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_data))
		{
			$query = $this->_buildQuery();
			$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
		}

		return $this->_data;
	}
    /**
     *
     * @return array $pagination
     */
    function getPagination()
    {
            // Lets load the content if it doesn't already exist
            if (empty($this->_pagination))
            {
                    jimport('joomla.html.pagination');
                    $this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
            }

            return $this->_pagination;
    }

    /**
     *
     * @return int
     */
    function getTotal()  {

		// Lets load the content if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
    }

    /**
     *
     * @global object $mainframe
     * @return string
     */

    function _buildQuery()  {
        global $mainframe;

	$orderby	= $this->_buildContentOrderBy();

        $db =& JFactory::getDBO();

        $query = "SELECT * FROM #__gps_terrain"
                . $orderby
                ;

        return $query;
    }
        /**
         *
         * @global object $mainframe
         * @global string $option
         * @return string
         */
 	function _buildContentOrderBy()
	{
		global $mainframe, $option;

		$filter_order		= $mainframe->getUserStateFromRequest( $option.'filter_order',		'filter_order',		'title',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.'filter_order_Dir',	'filter_order_Dir',	'',		'word' );

		$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_Dir.' , title ';

		return $orderby;
	}

    /**
     *
     * @param string $id
     */
	function setId($id)
	{
		// Set weblink id and wipe data
		$this->_id		= $id;
		$this->_data	= null;
	}

    function save()  {
        // get post data
        $row =& JRequest::get( 'post' );
        $table = $this->getTable( 'gps_terrain' );
        $table->bind( $row );

        if (!$table->store()) {
            return JError::raiseWarning( $table->getError() );
            return TRUE;
        }
    }

    /**
     *
     * @param array $cid
     * @param int $publish
     * @return boolean
     */
	function publish($cid = array(), $publish = 1)
	{
		$user 	=& JFactory::getUser();

		if (count( $cid ))
		{
			JArrayHelper::toInteger($cid);
			$cids = implode( ',', $cid );

			$query = 'UPDATE #__gps_terrain'
				. ' SET published = '.(int) $publish
				. ' WHERE id IN ( '.$cids.' )'
				. ' AND ( checked_out = 0 OR ( checked_out = '.(int) $user->get('id').' ) )'
			;
			$this->_db->setQuery( $query );
			if (!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}

		return true;
	}

    /**
     *
     * @param array $cid
     * @return boolean
     */
	function delete($cid = array())
	{

		$result = false;

		if (count( $cid ))
		{
			JArrayHelper::toInteger($cid);
			$cids = implode( ',', $cid );

            //delete from DB
			$query = 'DELETE FROM #__gps_terrain'
				. ' WHERE id IN ( '.$cids.' )';
			$this->_db->setQuery( $query );
			if(!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}

		return true;
	}

}

?>
